package com.bitcoin.scheduler.repository;

import com.bitcoin.scheduler.domain.BccPriceInfoDetail;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.math.BigInteger;
import java.util.Date;
import java.util.List;

public interface BccPriceInfoDetailDao extends JpaRepository<BccPriceInfoDetail, String> {

    /**
     * 查询时间开始到现在指定分钟的数据
     *
     * @param beginDate 开始时间
     * @param time      时间戳 时分秒
     * @return
     */
    @Query(nativeQuery = true, value = "SELECT * FROM bcc_price_info_detail t WHERE t.bcc_id = :bccId AND  t.price_time >= :beginDate AND DATE_FORMAT(t.price_time,'%H:%i:%S') = :time")
    List<BccPriceInfoDetail> findByPriceTime(@Param("bccId") String bccId, @Param("beginDate") Date beginDate, @Param("time") String time);

    /**
     * 取得时间范围内所有存在数据的价格时间点
     *
     * @param beginDate 开始时间
     * @param endDate   结束时间
     * @return
     */
    @Query(nativeQuery = true, value = "SELECT UNIX_TIMESTAMP(e.price_time) FROM bcc_price_info_detail e WHERE e.price_time >= :beginDate AND e.price_time <= :endDate GROUP BY e.price_time")
    List<BigInteger> existsDatePointList(@Param("beginDate") Date beginDate, @Param("endDate") Date endDate);
}
